// pages/paySuccess/paySuccess.js
import api from '../../utils/api.js'
import {
    HTTP
} from '../../utils/http.js'
import { config } from '../../utils/config'
const http = new HTTP()
var app = getApp();
const paramData = {
    num: 1,
    page: 1,
    size: 6
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        showGoPayBtn: true, //是否显示 继续支付的按钮
        selectMenus: [],
        imgUrl: config.imgUrl,
        products: [], //推荐的商品-单品
        num: 0
    },
    backMeuns(e) {
        console.log(e)
        wx.navigateTo({
            url: 'pages/myAllOrderList/myAllOrderList',
        })

        //清空 缓存时间段
        wx.removeStorageSync('timeField1')
        wx.removeStorageSync('timeField2')
    },
    backShopList(e) {
        let alreadyMenusId = wx.getStorageSync('alreadyMenusId')
        let aaa = JSON.stringify(alreadyMenusId)
        console.log('aaaaaaaa', aaa)
        wx.navigateTo({
                url: 'pages/myAllOrderList/myAllOrderList'
            })
            // wx.reLaunch({
            //   url: `/pages/recommendShopList/recommendShopList?alreadyMenusId=${aaa}&type=2`
            // })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.againType == 1) { //从再来一单，支付成功的跳转
            console.log('从再来一单，支付成功的跳转')
            this.setData({
                showGoPayBtn: false
            })
        } else {
            this.compareMenus();
        }
        // 展示 推荐 商品
        this._recommendProducts();
    },

    //当日菜谱---选择的菜谱 与 已购买的菜谱 比较
    compareMenus() {
        let alreadyMenus = wx.getStorageSync('userMenus'); //已购买的菜谱
        let alreadyMenusId = wx.getStorageSync('alreadyMenusId') || []; //已购买的菜谱id
        let selectMenus = wx.getStorageSync('select-menus') // 在菜谱选择页面——本地存储的菜谱

        if (alreadyMenus) {
            alreadyMenus.forEach((ele, i) => {
                if (alreadyMenusId.indexOf(ele.id) == -1) {
                    alreadyMenusId.push(ele.id)
                }
            })
            console.log('alreadyMenusId', alreadyMenusId)
            selectMenus.forEach((ele, index) => {
                alreadyMenusId.forEach((sEle) => {
                    if (sEle == ele.id) {
                        selectMenus.splice(index, 1)
                    }
                })
            })
            wx.setStorageSync('select-menus', selectMenus)
            if (selectMenus.length == alreadyMenusId.length) {
                console.log('所有的菜谱 您都买了哦')
                this.setData({
                    showGoPayBtn: false
                })

            } else {
                selectMenus.forEach((ele, index) => {
                        alreadyMenusId.forEach((sEle, sIndex) => {
                            if (ele.id == sEle) {
                                ele.showMeuns = true // 代表当前菜谱已购买
                            }
                        })
                    })
                    //页面设置 未购买的菜谱展示
                this.setData({
                    selectMenus
                })

            }

            wx.setStorageSync('alreadyMenusId', alreadyMenusId)
        }


    },
    _recommendProducts() {
        let that = this;
        http.request({
            url: api.recommendProducts,
            data: paramData,
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
            },
            success: function(res) {
                if (res.code == 200) {
                    let products = res.data;
                    that.setData({
                        products
                    })
                }

            },
            fail: function(err) { // 错误反馈
                console.log(err)
            }
        });
    },
    //添加到 菜篮
    add(e) {
        console.log(e)
        let product_id = e.target.dataset.product_id
        let summary = e.target.dataset.summary
        let that = this;
        let num = this.data.num;
        wx.showLoading({});

        http.request({
            url: api.shoppingCartAdd,
            method: "POST",
            data: {
                user_id: wx.getStorageSync("user_id"),
                product_id: product_id,
                summary: summary,
            },
            success: (res) => {

                wx.hideLoading()
                if (res.data.code == 200) {
                    num = num + 1
                    console.log(num)
                    this.setData({
                        num
                    })
                    wx.showToast({
                        title: '添加成功',
                        duration: 1500
                    })
                };
            }
        })
    },
    //跳转到单品菜篮
    goShoppingCar() {
        wx,
        wx.switchTab({
            url: '/pages/shoppingCar/shoppingCar',

        })
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        //清除已购买的菜谱本地存储
        wx.removeStorageSync('userMenus')
    },
})